
import GameData, { NumData } from './GameData';
import Utils from './utils/Utils';

// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class NumItem extends cc.Component {
    @property(cc.Node)
    public numLabelNode: cc.Node = null;
    @property(cc.Label)
    public numLabel: cc.Label = null;
    @property(cc.Node)
    public meiyuan: cc.Node = null;
    @property(cc.Node)
    public xin: cc.Node = null;
    @property(cc.Node)
    public star: cc.Node = null;

    /**类型 */
    private _type: number;
    /**数据 */
    public _data: NumData;
    /**状态 */
    private _state: number = 0;

    /**行 */
    public row: number;
    /**列 */
    public col: number;




    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        // this.numLabelNode = this.node.getChildByName('numLabel');
        // this.numLabel = this.numLabelNode.getComponent(cc.Label);
        this.numLabel['_isBold'] = true;
        this.numLabel.cacheMode = cc.Label.CacheMode.BITMAP;
    }

    start() {

        // this.node.on(cc.Node.EventType.TOUCH_START, this.onClcik, this)
    }

    onDestroy() {
        this.node.off(cc.Node.EventType.TOUCH_START, this.onClcik, this)
    }

    private onClcik(e: cc.Event.EventTouch) {

    }

    public set type(type: number) {
        this._type = type;


        // this.iconImg.source = `icon2_${GameData.typeArr[type]}_png`;
    }

    public get type() {
        return this._type;
    }

    /**状态 */
    public set state(state: number) {
        this._state = state || 0;
        if (this.data && this.data.state != state) {
            this.data.state = state;
        }
        //状态
        if (state == 1) {//选中
            this.stopTween();
            this.node.color = new cc.Color(24, 200, 199);
            this.numLabelNode.active = false;
            this.meiyuan.active = false;
            this.star.active = false;
            this.xin.active = true;
        } else if (state == 2) {//bigon
            this.stopTween();
            this.node.color = new cc.Color(249, 201, 71);
            this.numLabelNode.active = false;
            this.meiyuan.active = false;
            this.xin.active = false;
            this.star.active = true;
        } else {
            this.numLabelNode.active = true;
            this.star.active = false;
            this.xin.active = false;
            this.node.color = new cc.Color(255, 255, 255);
        }
    }

    public get state() {
        return this._state;
    }

    /**设置数据 */
    public set data(data: NumData) {
        if (!data.state) data.state = 0;
        this._data = data;
        this.numLabel.string = `${data.num}`;//数字
        ///奖励图标
        if (data.award > 0) {
            this.meiyuan.active = data.state ? false : true;
            let str = data.award == 1 ? "ui/djainbi" : "ui/dameiyuan";
            Utils.loadSpeite(str, this.meiyuan.getComponent(cc.Sprite));
        }
        this.state = data.state;
    }

    public get data() {
        return this._data;
    }

    public get num() {
        if (this.data) return this.data.num;
    }

    /** */
    public hint() {
        let time: number = 0.2;
        this.numLabelNode.angle = 0;
        this.numLabelNode.scale = 1;
        this.numLabelNode.stopAllActions();
        cc.tween(this.numLabelNode).repeatForever(cc.tween().to(0.1, { scale: 1.2 }).to(time, { angle: -8 }).to(time * 2, { angle: 8 }).to(time, { angle: 0 }).to(0.1, { scale: 1 })).start();
    }



    public stopTween() {
        // this.node.stopAllActions();
        this.numLabelNode.stopAllActions();
        this.numLabelNode.angle = 0;
        this.numLabelNode.scale = 1;
    }



    /**重置 */
    public reset() {
        this.stopTween();
        this.type = null;
        this.row = null;
        this.col = null;
        this.node.scale = 1;
        this.node.opacity = 255;
    }

    // update (dt) {}
}


